﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Threading;
using OpenGado.Business.Exceptions;
using OpenGado.DATA.Entities;
using OpenGado.DATA;


namespace OpenGado.Business
{

    /// <summary>
    /// Business class for unit of Measure object, mostly acting as a proxy for the data control class. 
    /// 
    /// </summary>
    public class UnitOfMeasureBusiness
    {

        /// <summary>
        ///  Saves the unit
        /// </summary>
        /// <param name="unitOfMeasure"></param>
        public void Save(UnitOfMeasure unitOfMeasure)
        {
            try
            {
                UnitOfMeasureDataControl.Instance.Save(unitOfMeasure);
            }
            catch (Exception exception)
            {
                    throw new DbOperationException("Unknow error occured during save", exception);
              
            }
        }


        /// <summary>
        ///  Deletes the unit
        /// </summary>
        /// <param name="unitOfMeasure"></param>
        public void Delete(UnitOfMeasure unitOfMeasure)
        {
            // check if its assigned somewhere else if yes dont delete, throw error

            try
            {
                // check if its used anywhere 

            
                   UnitOfMeasureDataControl.Instance.Delete(unitOfMeasure);
              
               
            }
            catch (Exception exception)
            {
                
                throw new DbOperationException("error on delete", exception);
               
            }

        }

        /// <summary>
        /// Retrieves the list of all units
        /// </summary>
        /// <returns></returns>
        public IList<UnitOfMeasure> GetAllUnits()
        {
           
         return UnitOfMeasureDataControl.Instance.GetMeasures();
        }

        /// <summary>
        /// Gets a single unit object
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        public UnitOfMeasure GetUnitById(int unitId)
        {


            return UnitOfMeasureDataControl.Instance.GetById(unitId);
        }

    }
}
